import Toast from '@vant/weapp/toast/toast'
const app = getApp()

Page({
	data: {
		name: '' // 会员姓名
	},
	onLoad() {
		app.getUrl(this)
		app.getSystem(this)
	},
	/**
	 * @function 暂不注册，返回上一页
	 */
	cancelRegister() {
		wx.navigateBack()
	},
	/**
	 * @function 姓名输入框回调
	 * @param {*} t
	 */
	onNameChange(t) {
		console.log(t)
		this.setData({
			name: t.detail
		})
	},
	/**
	 * @function 获取用户手机号，并且进行绑定
	 * @param {*} data
	 * @returns
	 */
	getPhoneNumber(data) {
		console.log(data)
		if (this.data.name === '') {
			Toast('请先输入会员姓名')
			return
		}
		app.util.request({
			url: 'index/wxBind',
			method: 'POST',
			cachetime: '0',
			data: {
				user_id: app.globalData.userId,
				code: data.detail.code,
				name: this.data.name,
				appid: app.globalData.appId,
				openid: app.globalData.userMinaInfo.openid,
				unionid: app.globalData.userMinaInfo.unionid
			},
			testapi: app.siteInfo.siteroot_web,
			success: res => {
				console.log(res)
				if (res.data.code === 0) {
					Toast('会员注册成功')
					// 更新用户信息
					app.getUserInfo()
					setTimeout(() => {
						wx.navigateBack({
							delta: 1
						})
					}, 1500)
				}
			}
		})
	}
})
